package studentinformationmanagementsystem.servlet.student;


import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.service.StudentService;
import studentinformationmanagementsystem.service.impl.StudenServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;
import studentinformationmanagementsystem.utils.GetMD5;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


/**
 * @author 苗庆
 * @version 1.0
 */


@WebServlet("/student1/login.do")
public class Studentlogin extends BaseServlet {

        StudentService studentService=new StudenServiceImpl();

    //学生用户登陆
    public void StudenLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PrintWriter writer = response.getWriter();
        //用户名
        String username = request.getParameter("username");
        //密码
        String password = request.getParameter("password");

        //是否记住了密码  1记住了密码，2未记住密码
        String checkbox = request.getParameter("checkbox");

        //当前是记住的密码还是输入的密码  值为1是输入的密码，值为2是记住的密码
        String checkbox1 = request.getParameter("checkbox1");

        String password1 = "";

        //输入的密码
        if (checkbox1.equals("1")){
            password1 = GetMD5.string2MD5(password);
        }


        //记住的密码
        if (checkbox1.equals("2")){

            Cookie[] cookies = request.getCookies();

            if (cookies.length!=0 ||cookies!=null){

                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    if ("password".equals(cookie.getName())){
                        password1 = cookie.getValue();
                        break;
                    }
                }
            }
        }


        Student student = new Student();
        student.setStudentid(username);
        student.setPassword(password1);
        Student login = studentService.login(student);


          if (login != null){  //判断是否找到对象，如果找到对象说明账号密码正确
            //登录成功

              //判断当前是否记住了账号密码
            if(checkbox.equals("1")){
                //创建两个cookie用于记住账号密码
                Cookie name = new Cookie("username", username);
                Cookie  pawd = new Cookie("password", password1);
                //把登陆的用户信息放入进来
                //设置cookie有效时间
                name.setMaxAge(60 * 60 * 24 * 7);
                pawd.setMaxAge(60 * 60 * 24 * 7);
                //cookie生成的有效位置
                name.setPath("/studentinformationmanagementsystems");
                pawd.setPath("/studentinformationmanagementsystems");

                response.addCookie(name);
                response.addCookie(pawd);
            }else{//未选择记住账号密码则产生一个新的cookie,并且设置这个cookie只能存活一会，用于覆盖之前记住的账号密码
                //创建两个cookie用于记住账号密码
                Cookie name = new Cookie("username", username);
                Cookie pawd = new Cookie("password", password1);
                //设置cookie有效时间
                name.setMaxAge(0);
                pawd.setMaxAge(0);
                //cookie生成的有效位置
                name.setPath("/studentinformationmanagementsystems");
                pawd.setPath("/studentinformationmanagementsystems");

                response.addCookie(name);
                response.addCookie(pawd);
            }

              //获取浏览器的session
              HttpSession session = request.getSession();
              //把登陆的用户信息放入进来
              session.setAttribute("Studentlogin",login);
              //设置session时效一周
              session.setMaxInactiveInterval(60 * 60 * 24 * 7);
              writer.println(2);

        }else {   //登陆失败重新回到登陆页面
              writer.println(1);
        }
    }



    //注销学生用户
    public void NoStudenLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //销毁session对象
        request.getSession().invalidate();
        HttpSession session = request.getSession(false);
        //重定向回到登陆页面
        response.sendRedirect(request.getContextPath()+"/student/log/Login.html");
    }


    //学生重置密码
    public void resetPassword (HttpServletRequest request, HttpServletResponse response) throws IOException {

        //学号
        String studentid = request.getParameter("studentid");
        //身份证号
        String idnumber = request.getParameter("idnumber");
        //手机号
        String phone = request.getParameter("phone");

        Student student = new Student();
        student.setStudentid(studentid);
        student.setIdnumber(idnumber);
        student.setPhone(phone);

        //调用重置密码方法
        Map<String, String> map = studentService.resetPassword(student);


        if (map.get("code").equals("0")){    //"输入的身份证格式有误请检查后重新输入"

            response.getWriter().write("0");

        }else if(map.get("code").equals("1")){  //"未找到该学生对象，即学号、手机号、身份证号有误"

            response.getWriter().write("1");

        }else if (map.get("code").equals("2")){  //"重置密码成功"

            response.getWriter().write("2");

        }else {          //重置密码失败

            response.getWriter().write("3");

        }

    }

}
